import { message } from 'antd';
import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { fetchValidation } from '@/utils';
import {
  queryExpressList,
  queryWorkMode,
  queryLogistics,
  operSaveExpress,
  operEeleteExpress,
  operUpdateTypeExpress,
} from './service';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetchExpressList: Effect;
    fetchWorkMode: Effect;
    fetchLogistics: Effect;
    saveExpress: Effect;
    updateType: Effect;
    deleteExpress: Effect;
  };
  reducers: {
    save: Reducer;
    setWorkMode: Reducer;
    setExpress: Reducer;
  };
}

const Model: ModelType = {
  namespace: 'expressInfoList',

  state: {
    expressInfo: {
      list: [],
      count: 0,
    },
    setExpress: {
      count: 0,
      list: [],
    },
    workMode: {},
  },

  effects: {
    * fetchExpressList({ payload }, { call, put }) {
      const response = yield call(queryExpressList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: response.body || { list: [], count: 0 },
      });
    },

    * fetchWorkMode({ payload }, { call, put }) {
      const response = yield call(queryWorkMode, payload);
      const data = fetchValidation(response);

      yield data && put({
        type: 'setWorkMode',
        payload: response.body,
      });
    },

    * fetchLogistics({ payload }, { call, put }) {
      payload = {
        pageIndex: 1,
        pageSize: 10,
        ...payload,
      };
      const response = yield call(queryLogistics, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'setExpress',
        payload: response.body,
      });
    },

    * saveExpress({ payload: { search, ...query } }, { call, put }) {
      const response = yield call(operSaveExpress, query);
      const data = fetchValidation(response);

      if (data) {
        message.success('添加成功');
        yield put({
          type: 'fetchExpressList',
          payload: search,
        });
      }
    },

    * updateType({ payload: { search, ...query } }, { call, put }) {
      const response = yield call(operUpdateTypeExpress, query);
      const data = fetchValidation(response);

      if (data) {
        message.success('设置成功');
        yield put({
          type: 'fetchExpressList',
          payload: search,
        });
      }
    },

    * deleteExpress({ payload: { search, ...query } }, { call, put }) {
      const response = yield call(operEeleteExpress, query);
      const data = fetchValidation(response);

      if (data) {
        message.success('删除成功');
        yield put({
          type: 'fetchExpressList',
          payload: search,
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        expressInfo: payload,
      };
    },

    setWorkMode(state, { payload }) {
      return {
        ...state,
        workMode: payload,
      };
    },

    setExpress(state, { payload }) {
      return {
        ...state,
        setExpress: payload,
      };
    },
  },
};

export default Model;
